﻿using System;
using System.Collections.Generic;
using System.Text;

namespace gChartSharp
{
    public class GridLinesParam : IRenderable
    {
        public string Name { get { return "chg"; } }


        /// <summary>
        /// XAxis step size
        /// </summary>
        public int XAxisStepSize { get; set; }


        /// <summary>
        /// YAxis step size
        /// </summary>
        public int YAxisStepSize { get; set; }


        /// <summary>
        /// Length of line segment - Rounded to 1 decimal place
        /// </summary>
        /// 
        /// <remarks>
        /// The API only allows one decimal place which 
        /// is why we are rounding here.  In addition, since this 
        /// is an optional property let's make the member variable
        /// nullable so we can determine whether or not it has been
        /// initialized when the Render() method is called.  This 
        /// is hidden from the outside by coalescing the get to 
        /// return 0 if it is null.
        /// </remarks>
        public float LineSegmentLength
        {
            get { return mLineSegmentLength ?? 0; }
            set { mLineSegmentLength = (float)Math.Round(value, 1); }
        }
        private float? mLineSegmentLength = null;


        /// <summary>
        /// Length of blank segment - Rounded to 1 decimal place
        /// </summary>
        public float BlankSegmentLength
        {
            get { return mBlankSegmentLength ?? 0; }
            set { mBlankSegmentLength = (float)Math.Round(value, 1); }
        }
        private float? mBlankSegmentLength = null;


        /// <summary>
        /// Constructor to initialize required properties
        /// </summary>
        /// <param name="xAxisStepSize"></param>
        /// <param name="yAxisStepSize"></param>
        public GridLinesParam(int xAxisStepSize, int yAxisStepSize)
        {
            XAxisStepSize = xAxisStepSize;
            YAxisStepSize = yAxisStepSize;
        }


        /// <summary>
        /// Constructor to initialize all properties
        /// </summary>
        /// <param name="xAxisStepSize"></param>
        /// <param name="yAxisStepSize"></param>
        /// <param name="lineSegmentLength"></param>
        /// <param name="blankSegmentLength"></param>
        public GridLinesParam(int xAxisStepSize, int yAxisStepSize, float lineSegmentLength, float blankSegmentLength)
        {
            XAxisStepSize = xAxisStepSize;
            YAxisStepSize = yAxisStepSize;
            LineSegmentLength = lineSegmentLength;
            BlankSegmentLength = blankSegmentLength;
        }

        // The default constructor
        public GridLinesParam() { }


        /// <summary>
        /// Render the API text
        /// </summary>
        /// <returns></returns>
        public string Render()
        {
            StringBuilder sb = new StringBuilder();

            if (this.BlankSegmentLength != default(float) || this.LineSegmentLength != default(float))
            {
                sb.AppendFormat("&{0}={1},{2}", Name, XAxisStepSize, YAxisStepSize);

                // if these optional properties were set, add them - remmeber to call the instance property
                // to get a properly rounded number
                if (mLineSegmentLength != null && mBlankSegmentLength != null)
                    sb.AppendFormat(",{0},{1}", LineSegmentLength, BlankSegmentLength);
            }

            return sb.ToString();
        }
    }
}
